package com.bank.seckill.service.impl;

import com.bank.entity.Member;
import com.bank.entity.Seckill;
import com.bank.entity.SeckillLog;
import com.bank.seckill.mapper.GoodsMapper;
import com.bank.seckill.mapper.OrderMapper;
import com.bank.seckill.service.GoodsService;
import com.bank.seckill.service.OrderService;
import com.bank.um.service.Impl.MemberServiceImpl;
import com.bank.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2022/1/28 星期五 17:49
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, SeckillLog> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GoodsServiceImpl goodsService;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public SeckillLog SelcetOrderByIdCard(String memcardnum) {
        QueryWrapper<SeckillLog> queryWrapper = new QueryWrapper();
        queryWrapper.eq("memcardnum", memcardnum);
        SeckillLog order = orderMapper.selectOne(queryWrapper);
        return order;
    }

    @Transactional
    public int seckill(Member member, Seckill goods) {
        //Seckill good = goodsService.SelectOne(new QueryWrapper<Seckill>().eq("secid", goods.getSecid()));

        //这两处判断在controller层实现，通过redis查询，比直接读表和if判断更安全和快速
//        if (good.getQuantity() < 1) {
//            return -1;
//        }
//        if (member.getIssecin()=="1"){
//            return -1;
//        }
        goods.setQuantity(goods.getQuantity() - 1);
//        boolean updateOrNot = goodsService.update(new UpdateWrapper<Seckill>().setSql("quantity = quantity -1").eq("secid", goods.getSecid()).gt("quantity", 0));
//        if (!updateOrNot) {
//            return -1;
//        }
        goodsMapper.updateById(goods);
        if (goods.getQuantity() < 1) {
            redisTemplate.opsForValue().set("isQuantityEmpty" + goods.getSecid(), "1");
            return -1;
        }
        member.setIssecin("1");//更改用户秒杀信息
        SeckillLog order = new SeckillLog();
        order.setMemId(member.getId());
        order.setMemcardnum(member.getMemcardnum());
        order.setCreateDatatime(new Date());
        order.setSecid(goods.getSecid());
        order.setStatus(1);
        int row2 = orderMapper.insert(order);




        redisTemplate.opsForValue().set("order" + member.getId() + ":" + goods.getSecid(), order);
        //redisTemplate.opsForValue().set("order" + ":" + goods.getSecid(), order);




        return row2;
    }


    public SeckillLog SelcetOrderById(String seckillorderId) {
        QueryWrapper<SeckillLog> queryWrapper = new QueryWrapper();
        queryWrapper.eq("seckillorderId", seckillorderId);
        SeckillLog order = orderMapper.selectOne(queryWrapper);
        return order;
    }

    public SeckillLog SelcetOrderByName(String name) {
        QueryWrapper<SeckillLog> queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", name);
        SeckillLog order = orderMapper.selectOne(queryWrapper);
        return order;
    }

    public int add(String secId, Long memcardnum, String memId) {
        SeckillLog order = new SeckillLog();
        order.setSecid(secId);
        order.setMemcardnum(memcardnum);
        order.setMemId(memId);
        order.setStatus(0);
        order.setCreateDatatime(new Date());
        int row = orderMapper.insert(order);
        return row;
    }


    /*
    * 提供客户端轮询接口实现
    * */
    public String getResult(Member member, String secid) {




        SeckillLog order = orderMapper.selectOne(new QueryWrapper<SeckillLog>().eq("memId", member.getId()).eq("secid", secid));
        //SeckillLog order = orderMapper.selectOne(new QueryWrapper<SeckillLog>().eq("secid", secid));



        if (order != null) {
            return order.getSeckillorderid();
        } else if (redisTemplate.hasKey("isQuantityEmpty" + secid)) {
            return "-1";
        } else {
            return "0";
        }
    }


    public String createPath(Member member,String secid){
        String path = DigestUtils.md5DigestAsHex((UUIDUtil.uuid() + "1111111").getBytes());
        redisTemplate.opsForValue().set("seckillPath"+member.getId()+":"+secid,path,60, TimeUnit.SECONDS);
        return path;
    }

    public Boolean checkPath(String path,Member member,String secid){
        if (member==null||Long.valueOf(secid)<0|| StringUtils.isEmpty(path)){
            return false;
        }
        String redisPath = (String) redisTemplate.opsForValue().get("seckillPath" + member.getId() + ":" + secid);
        return path.equals(redisPath);
    }
}
